package org.noobie.photopicker.domain;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import org.noobie.photopicker.controller.AlbumController;
import org.noobie.photopicker.entity.Album;
import org.noobie.photopicker.entity.Photo;
import org.noobie.photopicker.listener.OnLocalAlbumListener;
import org.noobie.photopicker.listener.OnLocalRecentListener;

import java.util.List;

@SuppressWarnings("unchecked")
public class PhotoPickerDomain {
    private AlbumController albumController;

    public PhotoPickerDomain(Context context) {
        albumController = new AlbumController(context);
    }

    public void getRecent(final OnLocalRecentListener listener) {
        final Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                listener.onPhotoLoaded((List<Photo>) msg.obj);
            }
        };
        new Thread(() -> {
            List<Photo> photos = albumController.getCurrent();
            Message msg = handler.obtainMessage();
            msg.obj = photos;
            msg.sendToTarget();
        }).start();
    }

    public void updateAlbum(final OnLocalAlbumListener listener) {
        final Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                listener.onAlbumLoaded((List<Album>) msg.obj);
            }
        };
        new Thread(() -> {
            List<Album> albums = albumController.getAlbums();
            Message msg = handler.obtainMessage();
            msg.obj = albums;
            msg.sendToTarget();
        }).start();
    }

    public void getAlbum(final String name, OnLocalRecentListener listener) {
        final Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                listener.onPhotoLoaded((List<Photo>) msg.obj);
            }
        };
        new Thread(() -> {
            List<Photo> photos = albumController.getAlbum(name);
            Message msg = handler.obtainMessage();
            msg.obj = photos;
            msg.sendToTarget();
        }).start();
    }
}
